package pers.wchen.mybatis.generate.template;

import pers.wchen.mybatis.generate.util.FileUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者： wchen
 * @描述： 模板管理类
 * @创建时间： 2018/2/10 16:30
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
public class TemplateManager {
    private String[] templatePaths;
    private Map<String, StringBuffer> tempResultMap;

    public TemplateManager(String[] templatePaths) {
        this.templatePaths = templatePaths;
//        this.tempResultMap = new HashMap<String, StringBuffer>();
    }

    public TemplateManager() {
        try {
            String rootPath = this.getClass().getClassLoader().getResource("").toURI().getPath() + "template/";
            System.out.println("rootPath: " + rootPath);
            this.templatePaths = new String[]{
                    rootPath + "xml.tm",
                    rootPath + "service.tm",
                    rootPath + "serviceImpl.tm",
                    rootPath + "vo.tm",
                    rootPath + "dao.tm",
                    rootPath + "test.tm",
                    rootPath + "view.tm",
                    rootPath + "controller.tm"
            };
//            this.tempResultMap = new HashMap<String, StringBuffer>();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @描述: 模板读取
     * @参数：
     * @返回值：
     * @创建人: wchen
     * @创建时间: 2018/2/10
     * @修改人和其它信息:
     */
    private void getTemplate() {
        for(String path : templatePaths){
            StringBuffer sbf = FileUtil.readTemp(path);
            //获取文件名关键字
            String key = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
            tempResultMap.put(key, sbf);
        }
    }

    public Map<String, StringBuffer> getTempResultMap() {
        if(this.tempResultMap == null){
            tempResultMap = new HashMap<String, StringBuffer>();
            getTemplate();
        }
        return tempResultMap;
    }
}
